home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4953 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf bug??????
  5. Date: Sat, 10 Feb 1996 21:22:34 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9602102022.AA01265@dxmint.cern.ch>
  8. References: <4fimvo$82s@fnord.dfw.net>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. jtmcap@dfw.dfw.net (Jerry Jackson) writes:
  14.  
  15. >the following is a program compiled using microway ndp c/c++ compiler.
  16. >
  17. >#include <stdio.h>
  18. >#include <string.h>
  19. >
  20. >main()
  21. >{
  22. >    char str_1[] = "013196";
  23. >    char str_2[] = "13196";
  24. >    long res_1, res_2;
  25. >
  26. >    sscanf(str_1,"%d",&res_1);
  27. >    sscanf(str_2,"%d",&res_2);
  28. >
  29. >    printf("\nres_1 = %d",res_1);
  30. >    printf("\nres_2 = %d",res_2);
  31. >}
  32. >
  33. >the output looks like this:
  34. >
  35. >res_1 = 89
  36. >res_2 = 13196
  37. >
  38. >microway says that the leading zero causes sscanf to do an octal 
  39. >conversion on the integer.  i have not found any documentation to verify 
  40. >this.  also other compilers that i use return the value 13196 for both 
  41. >calls to sscanf.
  42. >
  43. >bug or undocumented feature?
  44.  
  45. BUG.  This is the correct behaviour for the %i conversion specifier,
  46. _not_ for %d.
  47.  
  48. The Microway sscanf is broken and you should ask them to fix it as soon as
  49. possible (or to show you the paragraph from the standard which describes
  50. the behaviour of their implementation :-)
  51.  
  52. OTOH, your code is broken, as well.  %d expects a pointer to int, but you
  53. supply a pointer to long.  Since this invokes undefined behaviour, your
  54. code cannot be used to prove that the Microway implementation is broken.
  55. Check your C book to find out the correct conversion descriptor for a long
  56. variable.
  57.  
  58. Dan
  59. -- 
  60. Dan Pop
  61. CERN, CN Division
  62. Email: danpop@mail.cern.ch 
  63. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  64.